package com.cjh.controller;

import com.cjh.entity.User;
import com.cjh.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final String USERNAME="admin";
    private static final String PASSWORD="123456";

    @PostMapping("/login")
    public User login(@RequestBody User user){

        if (Objects.equals(USERNAME,user.getUsername())&&Objects.equals(PASSWORD,user.getPassword())){

            user.setToken(JwtUtil.createToken(user.getUsername()));
            return user;
        }
        return null;
    }

    @PostMapping("/checkToken")
    public String checkToken(HttpServletRequest request){

        String token = request.getHeader("token");
        return JwtUtil.checkToken(token)==true?"校验成功":"校验失败";
    }

}
